﻿// 功能：
// 描述：
// 作者：温伟鹏
// 日期：2010/9/3 11:12:11

using System;
using System.Collections.Generic;
using System.Text;
using MeizuSDK.COM;
using MeizuSDK.Presentation;
using System.IO;
using System.Threading;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Diagnostics;

namespace Demo
{
    class CameraWindow:DemoWindowBase
    {
        private PictureBox picBox;
        private Button btn;
        private Camera camera;
        private Thread thInitializeCamera;

        public CameraWindow()
        {
            BackgroundColor = Color.FromArgb(16, 0, 16);

            camera = new Camera();
        }

        protected override void OnLoad(EventArgs e)
        {
            picBox = new PictureBox();
            picBox.Size = new System.Drawing.Size(Width, Height - ToolBar.HEIGHT);
            picBox.Location = new System.Drawing.Point(0, 0);
            picBox.Visible = false;
            Controls.Add(picBox);

            btn = new Button();
            btn.Size = new Size(200, 100);
            btn.Location = new Point(0, 0);
            Controls.Add(btn);

            base.OnLoad(e);

            ThreadStart method = new ThreadStart(InitializeCamera);
            thInitializeCamera = new Thread(method);
            thInitializeCamera.Start();
        }

        private void InitializeCamera()
        {
            WaitDialog.Begin(this);

            toolbar.SetTextButton(ToolBarButtonIndex.MiddleTextButton, true, true, "拍照");
            toolbar.SetTextButton(ToolBarButtonIndex.RightTextButton, true, true, "聚焦");

            if(camera!=null)
            {
                camera.PhotoFilePath = Path.Combine("\\Disk\\Photo\\Camera Album", "CameraTest.png");
                camera.PhotoSize = PhotoSize.QXGA;
                camera.Mode = CameraMode.Auto;
                camera.IsDrawPreviewFrame = true;

                camera.InitDevice(this.HWnd);

                camera.PreviewAreaAlphaValue = 5;

                camera.StartPreview();
            }

            WaitDialog.End();
        }

        protected override void toolbar_ButtonClick(object sender, ToolBar.ButtonEventArgs e)
        {
            if(e.Index == ToolBarButtonIndex.RightTextButton)
            {
                if (camera != null)
                {
                    camera.StartAF();

                    MessageBox.Show("对焦结束。");
                }
            }
            else if(e.Index == ToolBarButtonIndex.MiddleTextButton)
            {
                if(camera!=null)
                {
                    camera.TakePhoto();

                    camera.Dispose();

                    MessageBox.Show("拍照完成！");

                    toolbar.SetTextButton(ToolBarButtonIndex.MiddleTextButton, false, false, "");
                    toolbar.SetTextButton(ToolBarButtonIndex.RightTextButton, false, false, "");

                    picBox.Visible = true;
                    picBox.LoadFromFile(Path.Combine("\\Disk\\Photo\\Camera Album", "CameraTest.png"));
                    picBox.Invalidate();
                    picBox.Update();
                }
            }

            base.toolbar_ButtonClick(sender, e);
        }

        protected override void Dispose(bool disposing)
        {
            if (thInitializeCamera!=null)
            {
                thInitializeCamera.Join();
            }

            if(camera!=null)
            {
                camera.Dispose();
            }

            base.Dispose(disposing);
        }
    }
}
